package com.jiepos.mpos.biz.code.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.jiepos.mpos.biz.code.service.MenuCodeService;
import com.jiepos.mpos.common.entity.App_Menu_CdEntity;
import com.jiepos.mpos.core.controller.BaseController;
import com.jiepos.mpos.core.entity.Pagination;
/**
 * 
 * <p>标题: —— 菜单业务控制器</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @author 娄伟峰
 * @version 1.0
 * @since 2017年4月13日 上午9:24:35
 * @history（历次修订内容、修订人、修订时间等）
 */
@Controller
@RequestMapping("menucode")
public class MenuCodeController extends BaseController {

	@Autowired
	private MenuCodeService menuCodeService;

	/**
	 * 进入菜单信息列表页面
	 * 
	 * @param request
	 * @param response
	 * @param pagination
	 * @return
	 */
	@RequestMapping("/getMenuIndexList.do")
	public ModelAndView getMenuIndexList(HttpServletRequest request, HttpServletResponse response,
			Pagination pagination, App_Menu_CdEntity menuEntity,String menuPname) {
		ModelAndView model = new ModelAndView("/module/code/menu/menu_list");
		return super.getDetail(request, response, model);
	}
 
	/**
	 * 查询菜单列表信息json数据
	 * @param request
	 * @param response
	 * @param pagination
	 * @param menuEntity
	 * @param menuPname
	 * @return
	 */
	@RequestMapping("/getMenuListJson.do")
	public @ResponseBody Map getMenuListJson(HttpServletRequest request, HttpServletResponse response,
			Pagination pagination, App_Menu_CdEntity menuEntity,String menuPname) {
		return menuCodeService.getMenuPageList(pagination, request, menuEntity, menuPname);
	}
	
	/**
	 * 进入菜单信息详细页面
	 * @param request
	 * @param response
	 * @param pagination
	 * @param menuEntity
	 * @return
	 */
	@RequestMapping("/getMenuDetail.do")
	public ModelAndView getMenuDetail(HttpServletRequest request, HttpServletResponse response,String menu_uuid) {
		ModelAndView model = new ModelAndView("/module/code/menu/menu_detail");
		App_Menu_CdEntity menuEntity = menuCodeService.getMenuById(menu_uuid);
		model.addObject("menuEntity", menuEntity);
		// 判断是否存在父级菜单
		if(StringUtils.isNotEmpty(menuEntity.getSup_menu_uuid())){
			menuEntity = menuCodeService.getMenuById(menuEntity.getSup_menu_uuid());
			model.addObject("menuPname", menuEntity.getMenu_name());
		}
		return super.getDetail(request, response, model);
	}
	
	/**
	 * 获取系统菜单树
	 * @return
	 */
	@RequestMapping("/getMenuTree.do")
	@ResponseBody
	public List<Map<String, Object>> getMenuTree(){
		return menuCodeService.getMenuTree();
	}
 
	/**
	 * 保存菜单
	 * @param request
	 * @param response
	 * @param menuEntity
	 * @param menupname
	 * @return
	 */
	@RequestMapping("/saveMenu.do")
	public ModelAndView saveMenu(HttpServletRequest request, HttpServletResponse response,
			App_Menu_CdEntity menuEntity,String menuPname) {
		menuCodeService.saveMenu(menuEntity,super.getLoginUser(request, response));
		Map map=super.getLoginUser(request, response);	
		return new ModelAndView(new RedirectView("getMenuIndexList.do"), map);
	}

	/**
	 * 删除菜单信息
	 * @param request
	 * @param response
	 * @param menu_uuids 菜单uuid字符串，多个uuid之间用逗号分隔
	 * @return
	 */
	@RequestMapping("/deleteMenuByIds.do")
	public ModelAndView deleteMenuByIds(HttpServletRequest request, HttpServletResponse response, String menu_uuids) {
		menuCodeService.deleteMenuByIds(menu_uuids);
		Map map=super.getLoginUser(request, response);  
		return new ModelAndView(new RedirectView("getMenuIndexList.do"), map);
	}
	
	@RequestMapping("/getMenuTreeByRoleUuid.do")
	@ResponseBody
	public List<Map<String, Object>> getMenuTreeByRoleUuid(HttpServletRequest request){
		return menuCodeService.getMenuTreeByRoleUuid(request);
	}
}
